<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="用户微信登录后的唯一凭证" prop="openid">-->
<!--        <el-input-->
<!--          v-model="queryParams.openid"-->
<!--          placeholder="请输入用户微信登录后的唯一凭证"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="用户姓名" prop="FullName">-->
<!--        <el-input-->
<!--          v-model="queryParams.FullName"-->
<!--          placeholder="请输入用户姓名"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="用户性别" prop="Gender">
        <el-input
          v-model="queryParams.Gender"
          placeholder="请输入用户性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="注册时间，也就是用户第一次登录的时间" prop="registrationTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.registrationTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择注册时间，也就是用户第一次登录的时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最近一次登录时间(先不用）" prop="LastLoginTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.LastLoginTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择最近一次登录时间(先不用）">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="用户余额" prop="balance">-->
<!--        <el-input-->
<!--          v-model="queryParams.balance"-->
<!--          placeholder="请输入用户余额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="所属大学" prop="university">
        <el-input
          v-model="queryParams.university"
          placeholder="请输入用户所属大学"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="登录方式" prop="loginMethod">-->
<!--        <el-input-->
<!--          v-model="queryParams.loginMethod"-->
<!--          placeholder="请输入登录方式"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="所属学校(暂时没有)" prop="universityId">-->
<!--        <el-input-->
<!--          v-model="queryParams.universityId"-->
<!--          placeholder="请输入所属学校(暂时没有)"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="所属校区(暂时没有)" prop="campusId">-->
<!--        <el-input-->
<!--          v-model="queryParams.campusId"-->
<!--          placeholder="请输入所属校区(暂时没有)"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['usermanage:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['usermanage:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['usermanage:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['usermanage:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="userId" />
<!--      <el-table-column label="用户微信登录后的唯一凭证" align="center" prop="openid" />-->
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="用户昵称" align="center" prop="username" />
<!--      <el-table-column label="注册登录需密码，从微信获取账号，不需要密码" align="center" prop="password" />-->
<!--      <el-table-column label="用户姓名" align="center" prop="FullName" />-->
      <el-table-column label="性别" align="center" prop="Gender" />
      <el-table-column label="注册时间" align="center" prop="registrationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="最近一次登录时间(先不用）" align="center" prop="LastLoginTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.LastLoginTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="用户头像" align="center" prop="avatarImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatarImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="用户状态" align="center" prop="userStatus" />
<!--      <el-table-column label="用户余额" align="center" prop="balance" />-->
      <el-table-column label="是否认证" align="center" prop="authStatus" />
      <el-table-column label="所属大学" align="center" prop="university" />
<!--      <el-table-column label="登录方式" align="center" prop="loginMethod" />-->
<!--      <el-table-column label="所属学校(暂时没有)" align="center" prop="universityId" />-->
<!--      <el-table-column label="所属校区(暂时没有)" align="center" prop="campusId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['usermanage:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['usermanage:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生用户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户微信登录后的唯一凭证" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入用户微信登录后的唯一凭证" />
        </el-form-item>
        <el-form-item label="用户手机号，从微信端获取，也可注册后填入" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入用户手机号，从微信端获取，也可注册后填入" />
        </el-form-item>
        <el-form-item label="用户昵称，一般从微信获取" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户昵称，一般从微信获取" />
        </el-form-item>
        <el-form-item label="注册登录需密码，从微信获取账号，不需要密码" prop="password">
          <el-input v-model="form.password" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用户姓名，用于实名认证" prop="FullName">
          <el-input v-model="form.FullName" placeholder="请输入用户姓名，用于实名认证" />
        </el-form-item>
        <el-form-item label="用户性别，从微信登录端获取" prop="Gender">
          <el-input v-model="form.Gender" placeholder="请输入用户性别，从微信登录端获取" />
        </el-form-item>
        <el-form-item label="注册时间，也就是用户第一次登录的时间" prop="registrationTime">
          <el-date-picker clearable
            v-model="form.registrationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择注册时间，也就是用户第一次登录的时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最近一次登录时间(先不用）" prop="LastLoginTime">
          <el-date-picker clearable
            v-model="form.LastLoginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最近一次登录时间(先不用）">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户头像地址" prop="avatarImage">
          <image-upload v-model="form.avatarImage"/>
        </el-form-item>
        <el-form-item label="用户余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入用户余额" />
        </el-form-item>
        <el-form-item label="用户属于哪个大学" prop="university">
          <el-input v-model="form.university" placeholder="请输入用户属于哪个大学" />
        </el-form-item>
        <el-form-item label="登录方式" prop="loginMethod">
          <el-input v-model="form.loginMethod" placeholder="请输入登录方式" />
        </el-form-item>
        <el-form-item label="所属学校(暂时没有)" prop="universityId">
          <el-input v-model="form.universityId" placeholder="请输入所属学校(暂时没有)" />
        </el-form-item>
        <el-form-item label="所属校区(暂时没有)" prop="campusId">
          <el-input v-model="form.campusId" placeholder="请输入所属校区(暂时没有)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/usermanage/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生用户管理表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openid: null,
        phoneNumber: null,
        username: null,
        password: null,
        FullName: null,
        Gender: null,
        registrationTime: null,
        LastLoginTime: null,
        avatarImage: null,
        userStatus: null,
        balance: null,
        authStatus: null,
        university: null,
        loginMethod: null,
        universityId: null,
        campusId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        openid: [
          { required: true, message: "用户微信登录后的唯一凭证不能为空", trigger: "blur" }
        ],
        phoneNumber: [
          { required: true, message: "用户手机号，从微信端获取，也可注册后填入不能为空", trigger: "blur" }
        ],
        registrationTime: [
          { required: true, message: "注册时间，也就是用户第一次登录的时间不能为空", trigger: "blur" }
        ],
        userStatus: [
          { required: true, message: "用户状态：正常1，禁用0，注销2不能为空", trigger: "change" }
        ],
        balance: [
          { required: true, message: "用户余额不能为空", trigger: "blur" }
        ],
        loginMethod: [
          { required: true, message: "登录方式不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生用户管理列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        openid: null,
        phoneNumber: null,
        username: null,
        password: null,
        FullName: null,
        Gender: null,
        registrationTime: null,
        LastLoginTime: null,
        avatarImage: null,
        userStatus: null,
        balance: null,
        authStatus: null,
        university: null,
        loginMethod: null,
        universityId: null,
        campusId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生用户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getUser(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生用户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除学生用户管理编号为"' + userIds + '"的数据项？').then(function() {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('usermanage/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
